package com.sense.kukoo.biz_library.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_library.model.MySubscriptionsModel
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.bean.BookItemBean
import com.sense.kukoo.common.repo.bean.LibrarySubscriptionBean
import com.sense.kukoo.common.repo.bean.MySubscriptionsBean
import com.sense.kukoo.common.repo.bean.QueryPageBean

class MySubscriptionsViewModel : BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private var mIndex = 2
    private val mPageSize = 20

    val mMySubscriptions: MutableLiveData<ArrayList<MySubscriptionsBean>> = MutableLiveData()
    val enableLoadMore: MutableLiveData<Boolean> = MutableLiveData(true)
    val mSubscripedChangedPosition:MutableLiveData<Int> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun loadData() {
        request {
            val result = Repo.http.subscriptionQueryPage(1, mPageSize)
            if (result.success) {
                val data: QueryPageBean<LibrarySubscriptionBean> = result.data!!
                val records:List<LibrarySubscriptionBean> = data.records
                //
                mIndex=2
                if(data.last){
                    enableLoadMore.postValue(false)
                }else{
                    enableLoadMore.postValue(true)
                }
                //show data
                val list = arrayListOf<MySubscriptionsBean>()
                records.forEach {
                    list.add(MySubscriptionsBean(item = it))
                }
                mMySubscriptions.postValue(list)

            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun getMoreData() {
        request {
            val result = Repo.http.subscriptionQueryPage(mIndex, mPageSize)
            if (result.success) {
                val data: QueryPageBean<LibrarySubscriptionBean> = result.data!!
                val records:List<LibrarySubscriptionBean> = data.records
                //
                mIndex++
                if(data.last){
                    enableLoadMore.postValue(false)
                }
                //添加并更新数据
                val value = mMySubscriptions.value!!
                val newArr = arrayListOf<MySubscriptionsBean>()
                newArr.addAll(value)
                records.forEach {
                    newArr.add(MySubscriptionsBean(item = it))
                }
                mMySubscriptions.postValue(newArr)

            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun setSubscripedChanged(
        bean: MySubscriptionsBean,
        isSubscripted: Boolean,
        position: Int
    ) {
        toView(ACTION_SHOW_DIALOG)
        request {
            val result = Repo.http.subscription("" + bean.item.bookId)
            if (result.success){
                bean.isSubscripted = isSubscripted
                mSubscripedChangedPosition.postValue(position)
            } else {
                Log.e(TAG, "result=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }
}